package com.yvdelx.cyclowomen.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.yvdelx.R;
import com.yvdelx.cyclowomen.activity.listener.DayActivityListener;
import com.yvdelx.cyclowomen.mediation.CycleManagement;
import com.yvdelx.cyclowomen.object.Day;
import com.yvdelx.cyclowomen.object.EnumSensation;
import com.yvdelx.cyclowomen.utility.ActivityUtility;
import com.yvdelx.cyclowomen.utility.DateUtility;
import com.yvdelx.cyclowomen.utility.LogUtility;
import com.yvdelx.cyclowomen.utility.StringUtility;
import com.yvdelx.cyclowomen.utility.ViewUtility;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Day modification activity display day data modification form
 */
public class DayModificationActivity extends Activity {

    private final static Integer CST_ID_SENSATION_SPINNER = R.id.SensationChoiceSpinner;
    private final static Integer CST_ID_TEMPERATURE_EDIT_TEXT = R.id.temperatureEditText;
    private final static Integer CST_ID_COMMENT_EDIT_TEXT = R.id.commentEditText;
    private final static Integer CST_ID_POK_CHECK_BOX = R.id.pok_check_box;
    private final static Integer CST_ID_RATING_BAR = R.id.QuantityRatingBar;

    private static List<Activity> listModificationActivities = new ArrayList<Activity>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_modification);

        ActivityUtility.addActivity(this);

        listModificationActivities.add(this);

        // Get bundle from the previous activity
        Bundle bundleCalendarActivity = this.getIntent()
                .getBundleExtra(StringUtility.CST_BUNDLE);

        Spinner spinner = (Spinner) this.findViewById(R.id.SensationChoiceSpinner);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.sensation_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);

        if(bundleCalendarActivity == null){
            this.finish();
        } else {
            // Get date from the previous activity in the bundle
            Long dateLong = bundleCalendarActivity.getLong(StringUtility.CST_DATE);
            Boolean isModification = bundleCalendarActivity
                    .getBoolean(StringUtility.CST_IS_MODIFICATION);

            Date date = DateUtility.getDateFromLong(dateLong);

            Day day;
            if(isModification){
                // Call to CycleManagement to get the day with the date
                day = CycleManagement.getDay(date);

                // Put data in the view
                ViewUtility.setTextInEditText(this.findViewById(CST_ID_TEMPERATURE_EDIT_TEXT),
                        String.valueOf(day.getTemperature()));
                ViewUtility.setTextInTextView(this.findViewById(CST_ID_COMMENT_EDIT_TEXT),
                        day.getComment());
                if(day.getSensation() != null){
                    ViewUtility.setSelectItemOnSpinner(this.findViewById(CST_ID_SENSATION_SPINNER),
                            getStringResourcesFromSensation(day.getSensation()));
                }

                ViewUtility.setRatingOnRatingBar(this.findViewById(CST_ID_RATING_BAR),
                        day.getQuantity());

                ViewUtility.setValueCheckBox(this.findViewById(CST_ID_POK_CHECK_BOX),
                        day.getIsNominalCondition());

            } else {
                day = new Day();
                day.setDate(date);
            }

            ActivityUtility.setNewTitle(this, date);

            // Instantiate the listener
            DayActivityListener listener = new DayActivityListener(day,this);
            ViewUtility.setViewOnClickListener(findViewById(R.id.ValidateDayModificationButton), listener);
            ViewUtility.setViewOnClickListener(findViewById(R.id.question_pok_button), listener);

            this.setTitle(DateUtility.dateToString(day.getDate()));
        }
    }

    @Override
    public void onBackPressed() {
        // Close all modification activities before finish this activity
        listModificationActivities.remove(this);
        ActivityUtility.closeActivities(listModificationActivities);
        listModificationActivities.clear();
        this.finish();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.day_modification, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        return id == R.id.action_settings || super.onOptionsItemSelected(item);
    }

    /**
     * Get the String representation from EnumSensation
     * @param sensation EnumSensation
     * @return String representation
     */
    private String getStringResourcesFromSensation(EnumSensation sensation){
        String sensationStr = sensation.toString();
        LogUtility.debug(this.getClass(),"Sensation day : " + sensationStr);
        if(sensationStr.equals(EnumSensation.MENSES.toString())){
            return getResources().getString(R.string.Menses);
        } else if(sensationStr.equals(EnumSensation.HUMID.toString())){
            return getResources().getString(R.string.Humid);
        } else if(sensationStr.equals(EnumSensation.WET.toString())){
            return getResources().getString(R.string.Wet);
        } else if(sensationStr.equals(EnumSensation.LUBRICATE.toString())){
            return getResources().getString(R.string.Lubricate);
        }

        return getResources().getString(R.string.Dry);
    }

}
